<?php
/**
 * 文章内容
 */
namespace App\Http\Controllers\Admin;
use App\Common\Logs\Logs;
use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Articles;
use App\Models\WebsitesAdmin;
use App\Models\WebsitesTopic;
use App\Service\ArticlesService;
use App\Service\WebsitesService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class ArticlesController extends  Controller
{
    public function index(Request $request)
    {
        $category_id = $request->input("category_id",0);
        $websites_topic_id = $request->input("websites_topic_id",0);
        $website_id = $request->input("website_id",0);
        //获取网站数据
        $webSiteData = WebsitesService::getWebsiteData();
        return view('admin.articles.index')
            ->with('webSiteData', $webSiteData)
            ->with('category_id',$category_id)
            ->with('websites_topic_id',$websites_topic_id)
            ->with('website_id',$website_id);
    }

    /**
     * 分类列表数据
     * @return void
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit',self::ADMIN_PAGE_NUM);
        $server = new ArticlesService();
        $result = $server->getListData($request,$num);
        return Utills::resultData($result);
    }

    public function show(Request $request)
    {
        $id = $request->input('id',0);
        $data = Articles::where(['id' => $id])->first();
        return view('admin.articles.show')->with('data', $data);
    }

    /**
     * 添加展示
     * @return void
     */
    public function createForm()
    {
        $websiteData = WebsitesService::getWebsiteData(1);
        return view('admin.articles.create')->with('websiteData',$websiteData);
    }

    /**
     * 添加报存
     * @param Request $request
     * @return void
     */
    public function store(Request $request){
        $validator = Validator::make($request->all(), [
            'website_id'=>'required',
            'websites_topic_id'=>'required',
            'category_ids'=>'required',
            'title' => 'required',
            'content' => 'required',
        ], [
            'website_id.required' => '请选择网站',
            'websites_topic_id.required' => '请选择栏目',
            'category_ids.required' => '标题关键词不能为空',
            'title.required' => '标题不能为空',
            'content.required' => '内容不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $category_ids = $request->input('category_ids',"");
        $website_id = $request->input('website_id',"");
        $topic_id = $request->input('websites_topic_id',"");
        $title = $request->input('title',"");
        $content = $request->input('content',"");
        $thumbs = $request->input('cover_url',"");
        $content = trim($content);
        $server = new ArticlesService();
        $result = $server->store($category_ids,$website_id, $topic_id,$title,$content,$thumbs);
        return Utills::resultData($result);
    }

    public function editForm(Request $request)
    {
        $id = $request->input('id',0);
        $data = Articles::where(['id' => $id])->first();
        //获取栏目信息
        $topicName = WebsitesTopic::query()->where('id',$data['websites_topic_id'])->value('titles') ?? "";
        $data['topicName'] = $topicName;
        $websiteData = WebsitesService::getWebsiteData();
        return view('admin.articles.edit')->with('data', $data)->with('websiteData',$websiteData);
    }

    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required',
            'content' => 'required',
        ], [
            'title.required' => '标题不能为空',
            'content.required' => '内容不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id',0);
        $title = $request->input('title',"");
        $content = $request->input('content',"");
        $content = trim($content);
        $status = $request->input('status',"");
        $website_id = $request->input('website_id',0);
        $websites_topic_id = $request->input('websites_topic_id',0);
        $thumbs = $request->input('cover_url',"");
        $server = new ArticlesService();
        $result = $server->update($id,$title,$content,$status,$website_id,$websites_topic_id,$thumbs);
        return Utills::resultData($result);

    }

    /**
     * 生成ai标题/内容
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function content(Request $request)
    {
        $server = new ArticlesService();
        $result = $server->ai_content($request);
        return Utills::resultData($result);
    }

    /*
    * 释放
    */
    public function release(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new ArticlesService();
        $result = $server->release($id);
        return Utills::resultData($result);
    }

    /*
    * 删除
    */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new ArticlesService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request)
    {
        $idArr = $request->input('idList',"");
        $server = new ArticlesService();
        $result = $server->delAll($idArr);
        return Utills::resultData($result);
    }

    /**
     * 获取所有的七牛云图片入库
     * @return void
     */
    public function thumbs()
    {
        $server = new ArticlesService();
        $result = $server->thumbs();
    }
}
